package com.ykdl.member.views;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.image.ImageOptions;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HeatImageListItem extends RelativeLayout{
	
	private Context context;
	private CatalogBean  catalog;
	public HeatImageListItem(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();
	}

	public HeatImageListItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();
	}

	public HeatImageListItem(Context context, CatalogBean  catalog) {
		super(context);
		this.context = context;
		this.catalog = catalog;
		init();
	}
	
	private void init(){
		RelativeLayout  view = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.heat_image_list_item_view, null);
		AsyncImageView  iv_heat_image = (AsyncImageView) view.findViewById(R.id.iv_heat_image);
		TextView  tv_heat_image_title = (TextView) view.findViewById(R.id.tv_heat_image_title);
		final String url = getHeatImageUrl();
		if(url != null){
			iv_heat_image.setUrl(((HomeActivity)context).TAG, url, ImageOptionsFactory.getBigPortraitOptions(context));
		}
		tv_heat_image_title.setText(catalog.getTitle());
		
		addView(view);
	}
	
	private String getHeatImageUrl(){
		if(catalog.getCatalogItems().get(0) != null){
			CatalogItemBean  item = catalog.getCatalogItems().get(0);
			if(item.getItem_type() == 1 || item.getItem_type() == 3){
				CatalogTopicItem topic = (CatalogTopicItem)item;
				if(topic.getItem().getFile_metas() != null){
					return topic.getItem().getFile_metas().get(0).getDownload_urls().getBig().getUrl();
				}
			}else if(item.getItem_type() == 2){
				CatalogArticleItem article = (CatalogArticleItem)item;
				if(article.getItem().getFile_meta() != null){
					return article.getItem().getFile_meta().getDownload_urls().getBig().getUrl();
				}
			}
				
		}
		return null;
	}
	
}
